<?php 

namespace app\seller\controller;
use app\common\model\AreaRegion;
use app\common\model\ShippingArea as ShippingAreaModel;
use app\seller\controller\Base;
use app\seller\validate\ShippingAreaValidate;
use app\seller\event\ShippingAreaEvent;
use app\common\utils\JsonService;
use think\facade\View;

class ShippingArea extends Base{

    /**
    * 新增物流配置
    * @return mixed
    */
    public function create(){
        if($this->request->isajax()){
            $post = $this->request->post();
            $validate = new ShippingAreaValidate;
            if(!$validate->scene('add')->check($post)) {
                return JsonService::error($validate->getError());
            }

            $post['shop_id'] = $this->shop_id;
            return ShippingAreaEvent::createShippingArea($post);
        }

        $shipping_id = $this->request->param('shipping_id');
        if(empty($shipping_id)){
            throw new \think\exception\RouteNotFoundException();
        }
        $this->assign('shipping_id',$shipping_id);
        //获取省市
        $province = getProvinceCity();
        $this->assign('province',$province);
        return View::fetch();
    }

    /**
    * 编辑物流配置
    * @return mixed
    */
    public function update(){
        if($this->request->isAjax()){
            $post = $this->request->post();
            $validate = new ShippingAreaValidate;
            if(!$validate->scene('edit')->check($post)) {
                return JsonService::error($validate->getError());
            }
            $post['shop_id'] = $this->shop_id;
            return ShippingAreaEvent::updateShippingArea($post);
        }

        $shipping_area_id = $this->request->get('shipping_area_id');
        $model = $this->findModel($shipping_area_id);
        $model['config'] = unserialize($model['config']);

        $area_list = AreaRegion::alias('a')
                    ->field('a.*,b.name')
                    ->join('Region b','a.region_id=b.id')
                    ->where(['a.shipping_area_id'=>$shipping_area_id])
                    ->select()
                    ->toArray();
        //获取省市
        $province = getProvinceCity();
        return View::fetch('update',[
            'model'=>$model,
            'province'=>$province,
            'area_list'=>$area_list,
        ]);
    }

    /**
    * 物流配置列表

    * @return mixed
    */
    public function index(){
        $params = array_filter(request()->param());
        $shipping_id = $this->request->param('shipping_id');
        if(empty($shipping_id)){
            throw new \think\exception\RouteNotFoundException();
        }
        $this->assign('shipping_id',$shipping_id);
        $params['shop_id'] = $this->shop_id;
        $params['shipping_id'] = $shipping_id;
        $dataProvider = ShippingAreaEvent::shippingAreaList($params);
        return View::fetch('index',['dataProvider'=>$dataProvider]);
    }


    /*
    * 删除物流配置
    */
    public function delete(){
        if($this->request->isAjax()){
            $shipping_area_id = $this->request->param('shipping_area_id');
            try{
                $model = $this->findModel($shipping_area_id);
                $model->delete();
                AreaRegion::delByWhere(['shipping_area_id'=>$shipping_area_id,'shop_id'=>$this->shop_id]);
            }catch (\Exception $ex){
                return JsonService::error($ex->getMessage());
            }
            return JsonService::success('删除成功');
        }
    }

    /**
    * 得到模型
    *
    * @param int $id
    * @param array $where
    *
    * @return array|false|\PDOStatement|string|\think\Model
    */
    public function findModel($id,$where = [])
    {
        if (empty($id) && empty($where)) {
            throw new \think\exception\RouteNotFoundException();
        }
        return ShippingAreaModel::findModel($id, $where);
    }

}
